public class L59 {

    public static void main(String[] args) {

        int n = 5;
        int[][] ints = new Solution().generateMatrix(n);
        System.out.println(ints);


    }


    static class Solution {
        public int[][] generateMatrix(int n) {

            int[][] ints = new int[n][n];

            int xIndex = 0, yIndex = 0;

            int count = 1;
            // 循环圈数
            int qs = n / 2;

            // 循环边界索引
            int xh = 0;

            while (qs > 0) {

                while (yIndex < n - xh - 1) {
                    ints[xIndex][yIndex] = count;
                    count++;
                    yIndex++;
                }

                while (xIndex < n - xh - 1) {
                    ints[xIndex][yIndex] = count;
                    count++;
                    xIndex++;
                }

                while (yIndex > xh) {
                    ints[xIndex][yIndex] = count;
                    count++;
                    yIndex--;
                }

                while (xIndex > xh) {
                    ints[xIndex][yIndex] = count;
                    count++;
                    xIndex--;
                }
                qs--;
                xIndex++;
                yIndex++;
                xh++;
            }

            if (n % 2 == 1) {
                ints[xIndex][yIndex] = count;
            }

            return ints;
        }
    }
}
